- Final Project: some more details
- Shiny Part II
- Example: Visualizing the World Development Indicators in Shiny
## Loading required package: sp
## rgdal: version: 1.4-8, (SVN revision 845) ## Geospatial Data Abstraction Library extensions to R successfully loaded ## Loaded GDAL runtime: GDAL 2.4.2, released 2019/06/28 ## Path to GDAL shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/gdal ## GDAL binary built with GEOS: FALSE ## Loaded PROJ.4 runtime: Rel. 5.2.0, September 15th, 2018, [PJ_VERSION: 520] ## Path to PROJ.4 shared files: /Library/Frameworks/R.framework/Versions/3.6/Resources/library/rgdal/proj ## Linking to sp version: 1.3-2
## ## Attaching package: 'shinydashboard'
## The following objects are masked from 'package:flexdashboard': ## ## renderValueBox, valueBox, valueBoxOutput
## The following object is masked from 'package:graphics': ## ## box
Beside the visualizations, your process book, code and data should be linked from the web site as well. Make sure to cite any code, ideas, and other insights you used.
Please start a new RStudio session by opening the file wdi-shiny_part1.Rproj to follow along in the tutorial.
ui <- fluidPage()
## <div class="container-fluid"></div>
sliderInput(inputId = "num", label = "Choose a number", value = 25, min = 1, max = 100)
## <div class="form-group shiny-input-container"> ## <label class="control-label" for="num">Choose a number</label> ## <input class="js-range-slider" id="num" data-min="1" data-max="100" data-from="25" data-step="1" data-grid="true" data-grid-num="9.9" data-grid-snap="false" data-prettify-separator="," data-prettify-enabled="true" data-keyboard="true" data-data-type="number"/> ## </div>
plotOutput("myPlot")
## <div id="myPlot" class="shiny-plot-output" style="width: 100% ; height: 400px"></div>
ui() part of your app creates HTML representations of your R functionsfluidPage() is a quick set up of the UI to call the output of a complete HTML page to the browserfluidRow()fluidRow() and column() simply divide the page into rows and columns. Think of a table (or grid) as a visual analogue.column()column() adds columns within a row. Each new column goes to the left of the previous column.fluidRow and columnui <- fluidPage(
fluidRow(
column(width = 4,
"width = 4"
),
column(width = 3, offset = 2,
"width 3, offset 2"
)
)
)
shinyApp(ui, server = function(input, output) { })
wellPanel()ui <- fluidPage(
wellPanel(
sliderInput(inputId = "num",
label = "Choose a number",
value = 25, min = 1, max = 100),
textInput(inputId = "title",
label = "Write a title",
value = "Histogram of Random Normal Values")
),
plotOutput("hist")
)
server <- function(input, output) {
output$hist <- renderPlot({
hist(rnorm(input$num), main = input$title)
})
}
shinyApp(ui = ui, server = server)
wellPanel()tabsetPanel() function.tabsetPanel()ui <- fluidPage(title = "Random generator",
tabsetPanel(
tabPanel(title = "Normal data",
plotOutput("norm"),
actionButton("renorm", "Resample")
),
tabPanel(title = "Uniform data",
plotOutput("unif"),
actionButton("reunif", "Resample")
),
tabPanel(title = "Chi Squared data",
plotOutput("chisq"),
actionButton("rechisq", "Resample")
)
)
)
server <- function(input, output) {# Excluded for brevity}
shinyApp(server = server, ui = ui)
tabsetPanel()tabsetPanel() - with headersnavlistPanel()navlistPanel()navlistPanel()ui <- fluidPage(title = "Random generator",
navlistPanel(
tabPanel(title = "Normal data",
plotOutput("norm"),
actionButton("renorm", "Resample")
),
tabPanel(title = "Uniform data",
plotOutput("unif"),
actionButton("reunif", "Resample")
),
tabPanel(title = "Chi Squared data",
plotOutput("chisq"),
actionButton("rechisq", "Resample")
)
)
)
server <- function(input, output) { # omitted for brevity }
shinyApp(server = server, ui = ui)
navlistPanel()sidebar() layoutui <- fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput(inputId = "num",
label = "Choose a number",
value = 25, min = 1, max = 100),
textInput(inputId = "title",
label = "Write a title",
value = "Histogram of Random Normal Values")
),
mainPanel(
plotOutput("hist")
)
)
)
server <- function(input, output) { # omitted for brevity}
shinyApp(ui = ui, server = server)
sidebar() layoutnavbarPage() function helps us to create applications with multiple top-level components.navbarPage() replaces fluidPage(). Requires a title.navbarPage()ui <- navbarPage(title = "Random generator",
tabPanel(title = "Normal data",
plotOutput("norm"),
actionButton("renorm", "Resample")
),
navbarMenu(title = "Other data",
tabPanel(title = "Uniform data",
plotOutput("unif"),
actionButton("reunif", "Resample")
),
tabPanel(title = "Chi Squared data",
plotOutput("chisq"),
actionButton("rechisq", "Resample")
)
)
)
server <- function(input, output) { # omitted for brevity}
shinyApp(server = server, ui = ui)
navbarPage()There are two shiny add-on packages that provide ready-made layouts for Shiny dashboard pages: flexdashboard and shinydashboard.
Examples: Neighboorhood Diversity, Linked Brushing, Multi Dropdown Data Explorer
Examples: CRAN downloads streaming, Flights Drill down with dynamic tabs and DB connection, Bus map
shinydashboard::dashboardPage()There are three ways Shiny works with CSS. To get CSS into your Shiny App, you can:
www directorywww directorywww folder of your app.www directoryui <- fluidPage(
theme = "bootstrap_slate.css", # Saved in the www folder
tags$h1("My application", style = "color:#DAA520;"), # CSS Style passed directly
titlePanel("My Application"),
sidebarLayout(
sidebarPanel(
sliderInput("obs",
"Number of observations:",
min = 0,
max = 1000,
value = 500)),
mainPanel(
plotOutput("distPlot")
)
)
)
server <- function(input, output) { # omitted for brevity}
shinyApp(ui, server)
Visualizing the World Development Indicators with a time-series graph and a leaflet map.
Please start a new RStudio session by opening the file wdi-shiny_part2.Rproj to follow along in the tutorial. Part II starts at line 480.
The Shiny website has plenty of tutorials, code for small examples, a gallery of full projects etc. Check it out.
About 180 Demo Examples are included in the shiny package and can be called directly from R and are all hosted.
For your convenience, I have also added them to this week’s data folder. Let’s take a look.
http://gallery.shinyapps.io/086-bus-dashboard
shiny::runApp("data/shiny_examples/086-bus-dashboard")
http://gallery.shinyapps.io/063-superzip-example
shiny::runApp("data/shiny_examples/063-superzip-example")
http://gallery.shinyapps.io/051-movie-explorer
shiny::runApp("data/shiny_examples/051-movie-explorer")